package com.tanqing.ddd.domain.utils;


import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.util.ObjectUtils;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * @author cyl
 */
public class DateUtils {

    /**
     * 仅显示年月日，例如 2015-08-11.
     */
    public static final String DATE_FORMAT = "yyyy-MM-dd";

    public static final String DATE_FORMAT_SHORT = "yyyy/MM/dd HH:mm";
    /**
     * 显示年月日时分秒，例如 2015-08-11 09:51:53.
     */
    public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATETIME_FORMAT_2 = "yyyyMMddHHmmss";
    public static final String DATETIME_FORMAT_3 = "yyyyMMdd";


    /**
     * 仅显示时分秒，例如 09:51:53.
     */
    public static final String TIME_FORMAT = "HH:mm:ss";

    /**
     * 每天的毫秒数 8640000.
     */
    public static final long MILLISECONDS_PER_DAY = 86400000L;

    /**
     * 每周的天数.
     */
    public static final long DAYS_PER_WEEK = 7L;

    /**
     * 每小时毫秒数.
     */
    public static final long MILLISECONDS_PER_HOUR = 3600000L;

    /**
     * 每分钟秒数.
     */
    public static final long SECONDS_PER_MINUTE = 60L;

    /**
     * 每小时秒数.
     */
    public static final long SECONDS_PER_HOUR = 3600L;

    /**
     * 每天秒数.
     */
    public static final long SECONDS_PER_DAY = 86400L;

    /**
     * 每个月秒数，默认每月30天.
     */
    public static final long SECONDS_PER_MONTH = 2592000L;

    /**
     * 每年秒数，默认每年365天.
     */
    public static final long SECONDS_PER_YEAR = 31536000L;

    /**
     * 常用的时间格式.
     */
    private static final String[] PARSE_PATTERNS = {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy/MM/dd",
            "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm"};

    private DateUtils() {
    }

    /**
     * 得到当前日期字符串.
     *
     * @return String 日期字符串，例如2015-08-11
     * @since 1.0
     */
    public static String getDate() {
        return getDate(DateUtils.DATE_FORMAT);
    }

    public static long getMilliseconds() {
        return System.currentTimeMillis();
    }

    public static Date formDate(String date) {
        return formDate(date, DATE_FORMAT);
    }

    public static Date second2Date(long second) {
        return new Date(second * 1000);
    }

    public static String millSecond2DateStr(long millSecond, String patter) {
        return formatDate(new Date(millSecond), patter);
    }

    public static Date formDate(String date, String pattern) {
        if (StringUtils.isEmpty(date)) {
            return null;
        }
        //指定转换格式
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date formDateTime(String date) {
        String pattern = DATETIME_FORMAT;
        if (StringUtils.isEmpty(date)) {
            return null;
        }
        //指定转换格式
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 得到当前时间字符串.
     *
     * @return String 时间字符串，例如 09:51:53
     * @since 1.0
     */
    public static String getTime() {
        return formatDate(new Date(), DateUtils.TIME_FORMAT);
    }

    /**
     * 得到当前日期和时间字符串.
     *
     * @return String 日期和时间字符串，例如 2015-08-11 09:51:53
     * @since 1.0
     */
    public static String getDateTime() {
        return formatDate(new Date(), DateUtils.DATETIME_FORMAT);
    }

    /**
     * 获取当前时间指定格式下的字符串.
     *
     * @param pattern 转化后时间展示的格式，例如"yyyy-MM-dd"，"yyyy-MM-dd HH:mm:ss"等
     * @return String 格式转换之后的时间字符串.
     * @since 1.0
     */
    public static String getDate(String pattern) {
        return DateFormatUtils.format(new Date(), pattern);
    }

    /**
     * 获取指定日期的字符串格式.
     *
     * @param date    需要格式化的时间，不能为空
     * @param pattern 时间格式，例如"yyyy-MM-dd"，"yyyy-MM-dd HH:mm:ss"等
     * @return String 格式转换之后的时间字符串.
     * @since 1.0
     */
    public static String getDate(Date date, String pattern) {
        return DateFormatUtils.format(date, pattern);
    }

    /**
     * 获取日期时间字符串，默认格式为（yyyy-MM-dd）.
     *
     * @param date    需要转化的日期时间
     * @param pattern 时间格式，例如"yyyy-MM-dd" "HH:mm:ss" "E"等
     * @return String 格式转换后的时间字符串
     * @since 1.0
     */
    public static String formatDate(Date date, Object... pattern) {
        String formatDate;
        if (pattern != null && pattern.length > 0) {
            formatDate = DateFormatUtils.format(date, pattern[0].toString());
        } else {
            formatDate = DateFormatUtils.format(date, DateUtils.DATE_FORMAT);
        }
        return formatDate;
    }

    /**
     * 获取当前年份字符串.
     *
     * @return String 当前年份字符串，例如 2015
     * @since 1.0
     */
    public static String getYear() {
        return formatDate(new Date(), "yyyy");
    }

    /**
     * 获取当前月份字符串.
     *
     * @return String 当前月份字符串，例如 08
     * @since 1.0
     */
    public static String getMonth() {
        return formatDate(new Date(), "MM");
    }

    /**
     * 获取当前天数字符串.
     *
     * @return String 当前天数字符串，例如 11
     * @since 1.0
     */
    public static String getDay() {
        return formatDate(new Date(), "dd");
    }

    /**
     * 获取当前星期字符串.
     *
     * @return String 当前星期字符串，例如星期二
     * @since 1.0
     */
    public static String getWeek() {
        return formatDate(new Date(), "E");
    }

    /**
     * 将日期型字符串转换为日期格式.
     * 支持的日期字符串格式包括"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
     * "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm"
     *
     * @return Date
     * @since 1.0
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return org.apache.commons.lang3.time.DateUtils.parseDate(str.toString(), PARSE_PATTERNS);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 获取当前日期与指定日期相隔的天数.
     *
     * @param date 给定的日期
     * @return long 日期间隔天数，正数表示给定日期在当前日期之前，负数表示在当前日期之后
     * @since 1.0
     */
    public static long pastDays(Date date) {
        // 将指定日期转换为yyyy-MM-dd格式
        date = DateUtils.parseDate(DateUtils.formatDate(date, DateUtils.DATE_FORMAT));
        // 当前日期转换为yyyy-MM-dd格式
        Date currentDate = DateUtils.parseDate(DateUtils.formatDate(new Date(), DateUtils.DATE_FORMAT));
        long t = 0;
        if (date != null && currentDate != null) {
            t = (currentDate.getTime() - date.getTime()) / DateUtils.MILLISECONDS_PER_DAY;
        }
        return t;
    }

    /**
     * 获取当前日期指定天数之后的日期.
     *
     * @param num 相隔天数
     * @return Date 日期
     * @since 1.0
     */
    public static Date nextDay(int num) {
        Calendar curr = Calendar.getInstance();
        curr.set(Calendar.DAY_OF_MONTH, curr.get(Calendar.DAY_OF_MONTH) + num);
        return curr.getTime();
    }


    /**
     * 获取指定日期指定天数之后的日期.
     *
     * @param num  相隔天数
     * @param date 指定日期
     * @return Date 日期
     * @since 1.0
     */
    public static Date nextDay(Date date, int num) {
        Calendar curr = Calendar.getInstance();
        curr.setTime(date);
        curr.set(Calendar.DAY_OF_MONTH, curr.get(Calendar.DAY_OF_MONTH) + num);
        return curr.getTime();
    }

    /**
     * 获取指定日期指定分钟之后的日期.
     *
     * @param minute 相隔分钟
     * @param date   指定日期
     * @return Date 日期
     * @since 1.0
     */
    public static Date nextMinute(Date date, int minute) {
        Calendar curr = Calendar.getInstance();
        curr.setTime(date);
        curr.set(Calendar.MINUTE, curr.get(Calendar.MINUTE) + minute);
        return curr.getTime();
    }

    /**
     * 获取当前日期指定月数之后的日期.
     *
     * @param num 间隔月数
     * @return Date 日期
     * @since 1.0
     */
    public static Date nextMonth(int num) {
        Calendar curr = Calendar.getInstance();
        int month = curr.get(Calendar.MONTH);
        curr.set(Calendar.MONTH, month + num);
        if (month == curr.get(Calendar.MONTH)) {
            curr.set(Calendar.MONTH, month + num);
        }
        return curr.getTime();
    }

    /**
     * 获取当前日期指定年数之后的日期.
     *
     * @param num 间隔年数
     * @return Date 日期
     * @since 1.0
     */
    public static Date nextYear(int num) {
        Calendar curr = Calendar.getInstance();
        curr.set(Calendar.YEAR, curr.get(Calendar.YEAR) + num);
        return curr.getTime();
    }

    /**
     * 将 Date 日期转化为 Calendar 类型日期.
     *
     * @param date 给定的时间，若为null，则默认为当前时间
     * @return Calendar Calendar对象
     * @since 1.0
     */
    public static Calendar getCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        // calendar.setFirstDayOfWeek(Calendar.SUNDAY);//每周从周日开始
        // calendar.setMinimalDaysInFirstWeek(1); // 设置每周最少为1天
        if (date != null) {
            calendar.setTime(date);
        }
        return calendar;
    }

    /**
     * 计算两个日期之间相差天数.
     *
     * @param start 计算开始日期
     * @param end   计算结束日期
     * @return long 相隔天数
     * @since 1.0
     */
    public static long getDaysBetween(Date start, Date end) {
        // 将指定日期转换为yyyy-MM-dd格式
        start = DateUtils.parseDate(DateUtils.formatDate(start, DateUtils.DATE_FORMAT));
        // 当前日期转换为yyyy-MM-dd格式
        end = DateUtils.parseDate(DateUtils.formatDate(end, DateUtils.DATE_FORMAT));

        long diff = 0;
        if (start != null && end != null) {
            diff = (end.getTime() - start.getTime()) / DateUtils.MILLISECONDS_PER_DAY;
        }
        return diff;
    }

    public static long getDaysBetweenByTime(Date start, Date end) {
        // 将指定日期转换为yyyy-MM-dd格式
        start = DateUtils.parseDate(DateUtils.formatDate(start, DateUtils.DATETIME_FORMAT));
        // 当前日期转换为yyyy-MM-dd格式
        end = DateUtils.parseDate(DateUtils.formatDate(end, DateUtils.DATETIME_FORMAT));
        long diff = 0;
        if (start != null && end != null) {
            diff = (end.getTime() - start.getTime()) / DateUtils.MILLISECONDS_PER_DAY;
        }
        return diff;
    }

    /**
     * 计算两个日期之前相隔多少周.
     *
     * @param start 计算开始时间
     * @param end   计算结束时间
     * @return long 相隔周数，向下取整
     * @since 1.0
     */
    public static long getWeeksBetween(Date start, Date end) {
        return getDaysBetween(start, end) / DateUtils.DAYS_PER_WEEK;
    }

    /**
     * 获取与指定日期间隔给定天数的日期.
     *
     * @param specifiedDay 给定的字符串格式日期，支持的日期字符串格式包括"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss",
     *                     "yyyy-MM-dd HH:mm", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss",
     *                     "yyyy/MM/dd HH:mm"
     * @param num          间隔天数
     * @return String 间隔指定天数之后的日期
     * @since 1.0
     */
    public static String getSpecifiedDayAfter(String specifiedDay, int num) {
        Date specifiedDate = parseDate(specifiedDay);
        Calendar c = Calendar.getInstance();
        c.setTime(specifiedDate);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day + num);
        return formatDate(c.getTime(), DateUtils.DATE_FORMAT);
    }

    /**
     * 计算两个日期之前间隔的小时数.
     *
     * @param date1 结束时间
     * @param date2 开始时间
     * @return String 相差的小时数，保留一位小数
     * @since 1.0
     */
    public static String dateMinus(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            return "0";
        }
        long r = date1.getTime() - date2.getTime();
        DecimalFormat df = new DecimalFormat("#.0");
        double result = r * 1.0 / DateUtils.MILLISECONDS_PER_HOUR;
        return df.format(result);
    }

    /**
     * 获取当月第一天
     */
    public static Date getMonthFirstDay(Date date) {
        if (date == null) {
            date = new Date();
        }
        Calendar cale = Calendar.getInstance();
        cale.setTime(date);
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        cale.set(Calendar.HOUR_OF_DAY, 0);
        cale.set(Calendar.MINUTE, 0);
        cale.set(Calendar.SECOND, 0);
        cale.set(Calendar.MILLISECOND, 0);
        return cale.getTime();
    }

    /**
     * 获取当月最后一天
     */
    public static Date getMonthLastDay(Date date) {
        if (date == null) {
            date = new Date();
        }
        Calendar cale = Calendar.getInstance();
        cale.setTime(date);
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        cale.set(Calendar.HOUR_OF_DAY, 0);
        cale.set(Calendar.MINUTE, 0);
        cale.set(Calendar.SECOND, 0);
        cale.set(Calendar.MILLISECOND, 0);
        return cale.getTime();
    }

    /**
     * 获取当年第一天
     */
    public static Date getYearFirstDay(Date date) {
        if (date == null) {
            date = new Date();
        }
        Calendar cale = Calendar.getInstance();
        cale.setTime(date);
        cale.add(Calendar.YEAR, 0);
        cale.set(Calendar.DAY_OF_YEAR, 1);
        cale.set(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        cale.set(Calendar.HOUR_OF_DAY, 0);
        cale.set(Calendar.MINUTE, 0);
        cale.set(Calendar.SECOND, 0);
        cale.set(Calendar.MILLISECOND, 0);
        return cale.getTime();
    }

    /**
     * 获取当年最后一天
     */
    public static Date getYearLastDay(Date date) {
        if (date == null) {
            date = new Date();
        }
        Calendar cale = Calendar.getInstance();
        cale.setTime(date);
        cale.add(Calendar.YEAR, 1);
        cale.set(Calendar.DAY_OF_YEAR, 0);
        cale.set(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        cale.set(Calendar.HOUR_OF_DAY, 0);
        cale.set(Calendar.MINUTE, 0);
        cale.set(Calendar.SECOND, 0);
        cale.set(Calendar.MILLISECOND, 0);
        return cale.getTime();
    }

    /**
     * 获取当前季度 .
     *
     * @return Integer 当前季度数
     * @since 1.0
     */
    public static Integer getCurrentSeason() {
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH) + 1;
        int season = 0;
        int i3Key = 3;
        int i6Key = 6;
        int i9Key = 9;
        int i12Key = 12;
        if (month <= i3Key) {
            season = 1;
        } else {
            if (month <= i6Key) {
                season = 2;
            } else {
                if (month <= i9Key) {
                    season = i3Key;
                } else {
                    if (month <= i12Key) {
                        season = 4;
                    }
                }
            }
        }
        return season;
    }

    /**
     * 将以秒为单位的时间转换为其他单位.
     *
     * @param seconds 秒数
     * @return String 例如 16分钟前、2小时前、3天前、4月前、5年前等
     * @since 1.0
     */
    public static String getIntervalBySeconds(long seconds) {
        StringBuilder buffer = new StringBuilder();
        if (seconds < SECONDS_PER_MINUTE) {
            buffer.append(seconds).append("秒前");
        } else if (seconds < SECONDS_PER_HOUR) {
            buffer.append(seconds / SECONDS_PER_MINUTE).append("分钟前");
        } else if (seconds < SECONDS_PER_DAY) {
            buffer.append(seconds / SECONDS_PER_HOUR).append("小时前");
        } else if (seconds < SECONDS_PER_MONTH) {
            buffer.append(seconds / SECONDS_PER_DAY).append("天前");
        } else if (seconds < SECONDS_PER_YEAR) {
            buffer.append(seconds / SECONDS_PER_MONTH).append("月前");
        } else {
            buffer.append(seconds / DateUtils.SECONDS_PER_YEAR).append("年前");
        }
        return buffer.toString();
    }

    /**
     * getNowTimeBefore(记录时间相当于目前多久之前)
     *
     * @param seconds 秒
     * @author rlliu
     * @since 1.0
     */
    public static String getNowTimeBefore(long seconds) {
        StringBuilder buffer = new StringBuilder();
        buffer.append("上传于");
        int s3600Key = 3600;
        int s86400Key = 86400;
        int s604800Key = 604800;
        int s2592000Key = 2592000;
        int s31104000Key = 31104000;
        if (seconds < s3600Key) {
            buffer.append((long) Math.floor(seconds / 60.0)).append("分钟前");
        } else {
            if (seconds < s86400Key) {
                buffer.append((long) Math.floor(seconds / 3600.0)).append("小时前");
            } else {
                if (seconds < s604800Key) {
                    buffer.append((long) Math.floor(seconds / 86400.0)).append("天前");
                } else {
                    if (seconds < s2592000Key) {
                        buffer.append((long) Math.floor(seconds / 604800.0)).append("周前");
                    } else {
                        if (seconds < s31104000Key) {
                            buffer.append((long) Math.floor(seconds / 2592000.0)).append("月前");
                        } else {
                            buffer.append((long) Math.floor(seconds / 31104000.0)).append("年前");
                        }
                    }
                }
            }
        }
        return buffer.toString();
    }

    /**
     * getMonthsBetween(查询两个日期相隔的月份)
     *
     * @param startDate 开始日期1 (格式yyyy-MM-dd)
     * @param endDate   截止日期2 (格式yyyy-MM-dd)
     * @return int 月份
     */
    public static int getMonthsBetween(String startDate, String endDate) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(DateUtils.parseDate(startDate));
        c2.setTime(DateUtils.parseDate(endDate));
        int year = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
        int month = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
        return Math.abs(year * 12 + month);
    }

    /**
     * getDayOfWeek(获取当前日期是星期几)
     *
     * @param dateStr 日期
     * @return 星期几
     */
    public static String getDayOfWeek(String dateStr) {
        String[] weekOfDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Date date = parseDate(dateStr);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int num = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        return weekOfDays[num];
    }

    /**
     * sns 格式 如几秒前，几分钟前，几小时前，几天前，几个月前，几年后， ... 精细，类如某个明星几秒钟之前发表了一篇微博
     *
     * @param createTime 时间戳
     * @return xxx
     */
    public static String snsFormat(long createTime) {
        long now = System.currentTimeMillis() / 1000;
        long differ = now - createTime / 1000;
        String dateStr;
        int m60Key = 60;
        int s3600Key = 3600;
        int h24Key = 24;
        int d30Key = 30;
        if (differ <= m60Key) {
            dateStr = "刚刚";
        } else {
            if (differ <= s3600Key) {
                dateStr = (differ / m60Key) + "分钟前";
            } else {
                if (differ <= s3600Key * h24Key) {
                    dateStr = (differ / s3600Key) + "小时前";
                } else {
                    if (differ <= s3600Key * h24Key * d30Key) {
                        dateStr = (differ / (s3600Key * h24Key)) + "天前";
                    } else {
                        Date date = new Date(createTime);
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        dateStr = sdf.format(date);
                    }
                }
            }
        }
        return dateStr;
    }

    /**
     * showGroupFormat 格式 刚刚，小时前日期
     *
     * @param createTime 时间戳
     * @return xxx
     */
    public static String showGroupFormat(long createTime) {
        long now = System.currentTimeMillis() / 1000;
        long differ = now - createTime / 1000;
        String dateStr;
        int h24Key = 24;
        int s3600Key = 3600;
        if (differ <= s3600Key) {
            dateStr = "刚刚";
        } else {
            if (differ <= s3600Key * h24Key) {
                dateStr = (differ / s3600Key) + "小时前";
            } else {
                Date date = new Date(createTime);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                dateStr = sdf.format(date);
            }
        }
        return dateStr;
    }

    /**
     * 得到UTC时间，类型为字符串，格式为"yyyy-MM-dd HH:mm"
     * 如果获取失败，返回null
     *
     * @return xxx
     */
    public static String getUtcTimeStr() {
        StringBuilder utcTimeBuffer = new StringBuilder();
        // 1、取得本地时间：
        Calendar cal = Calendar.getInstance();
        // 2、取得时间偏移量：
        int zoneOffset = cal.get(Calendar.ZONE_OFFSET);
        // 3、取得夏令时差：
        int dstOffset = cal.get(Calendar.DST_OFFSET);
        // 4、从本地时间里扣除这些差量，即可以取得UTC时间：
        cal.add(Calendar.MILLISECOND, -(zoneOffset + dstOffset));
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        utcTimeBuffer.append(year).append("-").append(month).append("-").append(day);
        utcTimeBuffer.append(" ").append(hour).append(":").append(minute);
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            sdf.parse(utcTimeBuffer.toString());
            return utcTimeBuffer.toString();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static Date getTodayZero() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * LocalDate 转 String
     *
     * @param localDate LocalDate
     * @param formatStr 指定转换格式 如："yyyy-MM-dd"
     * @return
     */
    public static String getLocalToString(LocalDate localDate, String formatStr) {
        if (localDate == null || formatStr == null) {
            return null;
        }
        try {
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern(formatStr);
            String dateStr = localDate.format(fmt);
            return dateStr;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * String转LocalDate
     *
     * @param timeStr   timeStr
     * @param formatStr 指定转换格式 如："yyyy-MM-dd"
     * @return
     */
    public static LocalDate getStringToLocalDate(String timeStr, String formatStr) {
        if (timeStr == null || formatStr == null) {
            return null;
        }
        try {
            //指定转换格式
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern(formatStr);
            //进行转换
            LocalDate date = LocalDate.parse(timeStr, fmt);
            return date;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Date转String.
     *
     * @param date      需要格式化的时间，不能为空
     * @param formatStr 时间格式，例如"yyyy-MM-dd"，"yyyy-MM-dd HH:mm:ss"等
     * @return String 格式转换之后的时间字符串.
     * @since 1.0
     */
    public static String getDateToString(Date date, String formatStr) {
        if (date == null || formatStr == null) {
            return null;
        }
        try {
            SimpleDateFormat format = new SimpleDateFormat(formatStr);
            return format.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * String 转Date
     * timeStr与formatStr格式保持一致
     *
     * @param timeStr   需要格式化的时间，不能为空
     * @param formatStr 时间格式，例如"yyyy-MM-dd"，"yyyy-MM-dd HH:mm:ss"等
     */
    public static Date getStringToDate(String timeStr, String formatStr) {
        if (timeStr == null || formatStr == null) {
            return null;
        }
        try {
            SimpleDateFormat format = new SimpleDateFormat(formatStr);
            return format.parse(timeStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * LocalDateTime转String.
     *
     * @param localDateTime 需要格式化的时间，不能为空
     * @param formatStr     时间格式，例如"yyyy-MM-dd"，"yyyy-MM-dd HH:mm:ss"等
     * @return String 格式转换之后的时间字符串.
     * @since 1.0
     */
    public static String getLocalDateTimeToString(LocalDateTime localDateTime, String formatStr) {
        if (localDateTime == null || formatStr == null) {
            return null;
        }
        try {
            DateTimeFormatter df = DateTimeFormatter.ofPattern(formatStr);
            return df.format(localDateTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * String 转 LocalDateTime
     * timeStr与formatStr格式保持一致
     *
     * @param timeStr   需要格式化的时间，不能为空
     * @param formatStr 时间格式，例如"yyyy-MM-dd"，"yyyy-MM-dd HH:mm:ss"等
     */
    public static LocalDateTime getStringToLocalDateTime(String timeStr, String formatStr) {
        if (timeStr == null || formatStr == null) {
            return null;
        }
        try {
            DateTimeFormatter df = DateTimeFormatter.ofPattern(formatStr);
            return LocalDateTime.parse(timeStr, df);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 将时间字符串转换为LocalDateTime
     *
     * @param TimeStr
     * @return
     */
    public static LocalDateTime strToLDT(String TimeStr) {
        if (StringUtils.isEmpty(TimeStr)) {
            return null;
        }
        Date date = DateUtils.parseDate(TimeStr);
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zoneId);
    }

    public static String ldtToStr(LocalDateTime localDateTime) {
        if (ObjectUtils.isEmpty(localDateTime)) {
            return null;
        }
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATETIME_FORMAT);
        return dateTimeFormatter.format(localDateTime);
    }

    /**
     * @param date
     * @param second
     * @return
     */
    public static LocalDateTime addSecond(Date date, Integer second) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.SECOND, second);

        date = calendar.getTime();
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zoneId);
    }

    /**
     * @param date
     * @param day
     * @return
     */
    public static LocalDateTime addDay(Date date, Integer day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, day);

        date = calendar.getTime();
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zoneId);
    }

    /**
     * @param date
     * @param year
     * @return
     */
    public static LocalDateTime addYear(Date date, Integer year) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, year);

        date = calendar.getTime();
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zoneId);
    }

    /**
     * @param date
     * @param month
     * @return
     */
    public static LocalDateTime addMonth(Date date, Integer month) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, month);

        date = calendar.getTime();
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zoneId);
    }

//    /**
//     * 计算两个日期距离多少年多少月多少天
//     * @param startDate 开始日期
//     * @param endDate 结束日期
//     * @return 距离结束日期 年数月数天数 （2年6个月20天）或（6个月）或（20天）
//     */
//    public static String getTerm(LocalDate startDate, LocalDate endDate) {
//        String term = "";
//        if(ObjectUtils.isEmpty(startDate) && ObjectUtils.isEmpty(endDate)){
//            return "";
//        }
//        //开始日期不能为null
//        if(ObjectUtils.isEmpty(startDate)){
//            return "";
//        }
//        //结束日期为null ，重新赋值为当前日期
//        if(ObjectUtils.isEmpty(endDate)){
//            endDate = LocalDate.now();
//        }
//
//        Period period = Period.between(startDate, endDate);
//        int years = period.getYears();
//        int months = period.getMonths();
//        int days = period.getDays();
//        term = (years== 0 ? "" : years + "年" ) + (months == 0 ? "" : + months + "个月" ) + (days == 0 ? "" : days + "天");
//        return term;
//    }

    /**
     * @param date
     * @return
     */
    public static LocalDateTime dateToLocalDateTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zoneId);
    }


    public static Long getTimestamp(Date date){
        long timestamp = date.getTime(); // 将日期转换为时间戳，单位为毫秒
        return timestamp;
    }

    public static Date getDateByTimestamp(long timestamp ){
        Date date = new Date(timestamp);
        return  date;
    }

}